package com.zl.service.Impl;

import com.zl.mapper.SelMapper;
import com.zl.pojo.vo.AchievementVo;
import com.zl.pojo.vo.ElectiveVo;
import com.zl.pojo.vo.SelVo;
import com.zl.service.SelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author ：ZhangLei
 * @Data ：2021/5/21 11:20
 * @Description ：
 */
@Service
@Transactional
public class SelServiceImpl implements SelService {

    @Autowired
    private SelMapper selMapper;

    @Override
    public List<SelVo> getCourse(String couGrade, String couDepartment,
                                 String couDepartment2, String couWeek,
                                 String couTime, String stuAccount) {
//        将空字符串转换为null   空字符串在前面，防止报空指针异常
        if ("".equals(couGrade)) {
            couGrade = null;
        }
        if ("".equals(couDepartment)) {
            couDepartment = null;
        }
        if ("".equals(couDepartment2)) {
            couDepartment2 = null;
        }
        if ("".equals(couWeek)) {
            couWeek = null;
        }
        if ("".equals(couTime)) {
            couTime = null;
        }
        return selMapper.getCourse(couGrade, couDepartment, couDepartment2, couWeek, couTime, stuAccount);
    }


    /**
     *
     * 此方法只能在repository在本地代码处理才可以
     * 如果是feign远程调用则需要进行逆服务处理，需要获取异常时，手动调用逆服务处理
     *
     * 插入学生选课
     * @param stuAccount
     * @param couId
     * @param stuName
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertcouser(String stuAccount, String couId, String stuName,Integer couStuNum) {
        Date date  = new Date();
        /*//查询选课时间
        Semester semester = selMapper.selectData();
        //选课开始时间
        Date selStartTime = semester.getSelStartTime();
        //选课结束时间
        Date selOverTime = semester.getSelOverTime();
        if(date.compareTo(selStartTime) == -1){
            System.out.println("还未到选课时间");
        }else if(selOverTime.compareTo(selStartTime) == -1){
            System.out.printf("选课时间已经结束");
        }*/
        String courseFlag = "learn";
        Integer res = selMapper.upCourse(couId, ++couStuNum);
        Integer i = selMapper.insertcouser(stuAccount, couId, stuName, courseFlag, date);
        if (i.equals(res)){
            return 1;
        }
        return 0;
    }

    /**
     * 查询学生已选课程
     * @param stuAccount
     * @param courseFlag
     * @return
     */
    @Override
    public ElectiveVo selectele(String stuAccount, String courseFlag) {
        ElectiveVo selectele = selMapper.selectele(stuAccount, courseFlag);
        return selectele;
    }


    /**
     * 查询成绩
     * @param stuAccount
     * @param semester
     * @return
     */
    @Override
    public List<AchievementVo> selectach(String stuAccount, String semester) {
        return selMapper.selectach(stuAccount,semester);
    }

    @Override
    public List<String> selectSem() {
        return selMapper.selectSem();
    }


}
